package com.lwj.liwanjia;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.base.MyApplication;
import com.lwj.liwanjia.utils.BadgeUtils;
import com.lwj.liwanjia.utils.FragNavController;
import com.lwj.liwanjia.utils.PermissionUtils;
import com.lwj.liwanjia.utils.RxBus;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.fragment.chat.ChatFragment;
import com.lwj.liwanjia.view.fragment.personal.PersonalFragment;
import com.lwj.liwanjia.view.fragment.shop.ShopFragment;
import com.lwj.liwanjia.view.fragment.shouye.MianFragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 首页
 */
public class MainActivity extends BaseActivity implements OnTabSelectListener {

    @BindView(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @BindView(R.id.bottom_bar)
    BottomBar bottomBar;

    private FragNavController fragNavController;
    private List<Fragment> fragmentList = new ArrayList<>();
    private long firstTime = 0;

    @Override
    protected void initBefore(Bundle savedInstanceState) {
        super.initBefore(savedInstanceState);
        fragmentList.add(new MianFragment());
        fragmentList.add(new ChatFragment());
        fragmentList.add(new ShopFragment());
        fragmentList.add(new PersonalFragment());
        fragNavController = new FragNavController(savedInstanceState
                , getSupportFragmentManager()
                , R.id.fragment_container
                , fragmentList
                , FragNavController.TAB1);//必须设置成TAB1，否则一开始就会访问个人中心和购物车的接口同时
    }

    @Override
    protected void initView() {
        bottomBar.setOnTabSelectListener(this);
        PermissionUtils.requestPermissions(this);
    }

    @Override
    protected void initData() {
        String type = getIntent().getStringExtra("type");
        if (type != null && (type.equals("购物车") || type.equals("个人中心"))) {//从登录界面跳转过来
            if (type.equals("购物车")) {
                fragNavController.switchTab(FragNavController.TAB3);
                bottomBar.selectTabAtPosition(FragNavController.TAB3);
            } else if (type.equals("个人中心")) {
                fragNavController.switchTab(FragNavController.TAB4);
                bottomBar.selectTabAtPosition(FragNavController.TAB4);
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {//点击购物车小图标跳转
        super.onNewIntent(intent);
        String type = intent.getStringExtra("type");
        if (!TextUtils.isEmpty(type)) {
            if (type.equals("购物车")) {
                fragNavController.switchTab(FragNavController.TAB3);
                bottomBar.selectTabAtPosition(FragNavController.TAB3);
            }
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onTabSelected(@IdRes int tabId) {
        switch (tabId) {
            case R.id.tab_home: //首页
                fragNavController.switchTab(FragNavController.TAB1);
                break;
            case R.id.tab_chat://聊聊吧
                fragNavController.switchTab(FragNavController.TAB2);
                break;
            case R.id.tab_shop://购物车
                fragNavController.switchTab(FragNavController.TAB3);
                break;
            case R.id.tab_personal://个人中心
                fragNavController.switchTab(FragNavController.TAB4);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;
        } else {
            MyApplication.getInstance().exitApp();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        SpfUtils.getSpfUtils(this).setBadgeCoount(0);
        BadgeUtils.removeBadge(this);
    }
}
